package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.controller.vo.TenantVo;
import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.domain.TenantMeal;
import cn.itsource.hrm.mapper.EmployeeMapper;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.mapper.TenantMealMapper;
import cn.itsource.hrm.query.TenantQuery;
import cn.itsource.hrm.service.ITenantService;
import cn.itsource.util.PageList;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenyuqi
 * @since 2020-05-23
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private TenantMealMapper tenantMealMapper;
    @Override
    public PageList<Tenant> pageList(TenantQuery query) {
        IPage<Tenant> ipage = baseMapper.queryPage(new Page<Tenant>(query.getPage(), query.getRows()), query);
        return new PageList<>(ipage.getTotal(),ipage.getRecords());

    }

    @Override
    @Transactional
    public void register(TenantVo tenantVo) {
        //租户表中 - 返回主键
        Tenant tenant = new Tenant();
        //Spring提供的工具类，用于对象的拷贝
        BeanUtils.copyProperties(tenantVo,tenant);
        tenant.setRegisterTime(System.currentTimeMillis());
        tenant.setState(0);
        baseMapper.insert(tenant);
        //员工表中
        Long tenantId = tenant.getId();
        Employee employee = new Employee();
        BeanUtils.copyProperties(tenantVo,employee);
        employee.setTenantId(tenantId);
        employee.setInputTime(System.currentTimeMillis());
        employee.setState(1);
        employeeMapper.insert(employee);
        //租户套餐中间表中
        TenantMeal tenantMeal = new TenantMeal();
        tenantMeal.setTenantId(tenantId);
        tenantMeal.setMealId(tenantVo.getMeal());
        tenantMealMapper.insert(tenantMeal);
    }
}
